﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS2019.JobOrder
{
    public partial class JobOrder6 : Form
    {
        public string db = "", userName = "", pwd = "", hostname = "";
        private string result = "";

        public static string str;
        SqlConnection conn;//定义数据库连接对象


        public JobOrder6()
        {
            InitializeComponent();            
        }
        private void btnOpenDB_Click(object sender, EventArgs e)
        {
            db = txtDBName.Text.Trim();
            userName = txtUserName.Text.Trim();
            pwd = txtPassword.Text.Trim();
            hostname = txtHostName.Text.Trim();
            if (hostname.Length == 0)
            {
                MessageBox.Show("请输入服务器名称！", "系统提示");//消息弹窗
                this.txtHostName.Focus();//光标闪烁
            }
            if (db.Length == 0)
            {
                MessageBox.Show("请输入数据库名称！", "系统提示");//消息弹窗
                this.txtDBName.Focus();//光标闪烁
            }
            if (userName.Length == 0)
            {
                MessageBox.Show("请输入数据库登录用户！", "系统提示");//消息弹窗
                this.txtUserName.Focus();//光标闪烁
            }
            if (pwd.Length == 0)
            {
                MessageBox.Show("请输入数据库用户密码！", "系统提示");//消息弹窗
                this.txtPassword.Focus();//光标闪烁
            }
            if(hostname.Length!=0 || userName.Length!=0 || db.Length!=0 || pwd.Length != 0)
            {
                try
                {
                    //定义数据库连接字符串
                    str = "Server=" + hostname + ";User Id=" + userName + ";Pwd=" + pwd + ";DataBase=" + db;
                    //根据数据库连接字符串str实例化数据库连接对象conn
                    conn = new SqlConnection(str);

                    conn.Open();//打开数据库连接
                    if (conn.State == ConnectionState.Open)
                    {
                        result = "数据库已成功连接！";
                        txtResult.Text = result;
                    }
                }
                catch(Exception ex)
                {
                    txtResult.Text = ex.Message;
                }
            }
        }

        private void btnCloseDB_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Close();
                //判断当前数据库连接状态是否关闭
                if (conn.State == ConnectionState.Closed)
                {
                    txtResult.Text = "数据库已经成功关闭！";
                }
            }
            catch (Exception ex)
            {
                //将异常消息抛至消息输出内容框显示
                txtResult.Text = ex.Message;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtHostName.Text = "";
            txtDBName.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtResult.Text = "";
            MessageBox.Show("请在对应区域输入相关信息以完成数据库操作！", "系统提示：");

        }

    }
}
